﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DrugStorehouseMIS.QueryAndReport;
using DrugStorehouseMIS.BLL;
using DrugStorehouseMIS.Comm;

namespace DrugStorehouseMIS.StockInAndOutManager
{
    public partial class frmStockOutDetailEdit : Form
    {
        private StockOutHeader stockOutHeaderBLL;
        private StockOutDetail stockOutDetailBLL;
        private string aDrugName;
        private string aSepcifications;
        private string aMeasuirngUnit;
        private string aTradePrice;
        private string aRetailPrice;
        private string aStockQuantity;

        public frmStockOutDetailEdit()
        {
            InitializeComponent();
        }

        public void Init(StockOutHeader header,StockOutDetail detail,string drugName,string specifications,string measuringUnit,string tradePrice,string retailPrice,string stockQuantity)
        {
            stockOutHeaderBLL = header;
            stockOutDetailBLL = detail;
            aDrugName = drugName;
            aSepcifications = specifications;
            aMeasuirngUnit = measuringUnit;
            aTradePrice = tradePrice;
            aRetailPrice = retailPrice;
            aStockQuantity = stockQuantity;

            SetInfo();
        }

        public void SetInfo()
        {
            //设置header信息
            txtStockOutNum.Text = stockOutHeaderBLL.StockOutNum;
            txtStockOutDatetime.Text = Convert.ToString(stockOutHeaderBLL.StockOutDatetime);
            txtStockOutType.Text = stockOutHeaderBLL.StockOutTypeName.StockOutTypeName;
            txtDepartment.Text = stockOutHeaderBLL.DepartmentName.DepartmentName;

            //设置detail信息
            txtAmount.Text = stockOutDetailBLL.Amount.ToString();
            txtDrugNum.Text = stockOutDetailBLL.DrugNum.DrugNum.ToString();
            txtOutportPrice.Text = stockOutDetailBLL.OutportPrice.ToString();
            txtQuantity.Text = stockOutDetailBLL.RealQuantity.ToString();

            //设置药品信息
            txtDrugName.Text = aDrugName;
            txtMeasuringUnit.Text = aMeasuirngUnit;
            txtRetailPrice.Text = aRetailPrice;
            txtSpecifications.Text = aSepcifications;
            txtTradePrice.Text = aTradePrice;
            txtStockQuantity.Text = aStockQuantity;
        }

        public StockOutDetail GetObject()
        {
            StockOutDetail detail = new StockOutDetail();
            detail.DrugNum = new Drug();

            detail.Amount = Convert.ToSingle(txtAmount.Text.Trim());
            detail.DrugNum.DrugNum = Convert.ToInt32(txtDrugNum.Text.Trim());
            detail.OutportPrice = Convert.ToSingle(txtOutportPrice.Text.Trim());
            detail.RealQuantity = Convert.ToInt32(txtQuantity.Text.Trim());
            detail.StockOutNum = txtStockOutNum.Text.Trim();

            return detail;
        }

        public bool Check()
        {
            if (txtDrugNum.Text.Trim() != "" && txtOutportPrice.Text.Trim() != "" && txtQuantity.Text.Trim() != "")
            {
                return btnUpdate.Enabled = true;
            }
            else
            {
                return btnUpdate.Enabled = false;
            }
        }

        /// <summary>
        /// 获取选中的各种信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelect_Click(object sender, EventArgs e)
        {
            frmDrugInfo form = new frmDrugInfo();
            form.ShowDialog();
            if (form.DialogResult == DialogResult.OK)
            {
                txtDrugNum.Text = Convert.ToString(form.DrugBLL.DrugNum);
                txtDrugName.Text = form.DrugBLL.TradeName;
                txtSpecifications.Text = form.DrugBLL.Sepcifications;
                txtMeasuringUnit.Text = form.DrugBLL.MeasuringUnitNum.MeasuringUnitName;
                txtRetailPrice.Text = form.DrugBLL.RetailPrice.ToString();
                txtTradePrice.Text = form.DrugBLL.TradePrice.ToString();

                txtStockQuantity.Text = form.InventoryBLL.StockQuantity.ToString();
            }
            Error();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(txtQuantity.Text.Trim()) > Convert.ToInt32(txtStockQuantity.Text.Trim()))
            {
                MessageBox.Show("出库数量不能超过当前库存量！", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                stockOutDetailBLL.SetStockOutDetail(GetObject());
                DialogResult = DialogResult.OK;
            }            
        }

        private void btnRecover_Click(object sender, EventArgs e)
        {
            SetInfo();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtOutportPrice.Text = txtQuantity.Text = "";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtOutportPrice_TextChanged(object sender, EventArgs e)
        {
            Tool.OnlyNumber(txtOutportPrice, '.');
            this.Calcluate();
            this.Check();
        }

        private void txtQuantity_TextChanged(object sender, EventArgs e)
        {
            Tool.OnlyNumber(txtOutportPrice, '.');
            this.Calcluate();
            this.Check();
        }

        /// <summary>
        /// 计算金额
        /// </summary>
        public void Calcluate()
        {
            float price, amount;
            long qty;

            if (txtOutportPrice.Text.Trim() != "")
            {
                price = Convert.ToSingle(txtOutportPrice.Text.Trim());
            }
            else
            {
                price = 0;
            }
            if (txtQuantity.Text.Trim() != "")
            {
                qty = Convert.ToInt64(txtQuantity.Text.Trim());
            }
            else
            {
                qty = 0;
            }
            amount = price * qty;
            txtAmount.Text = Convert.ToString(amount);
        }

        /// <summary>
        /// 检查库存
        /// </summary>
        public void Error()
        {
            if (Convert.ToInt32(txtStockQuantity.Text) <= 0)
            {
                MessageBox.Show("库存量不足，不能出库！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtQuantity.Enabled = txtOutportPrice.Enabled = false;
            }
        }
    }
}